//
//  NaAnima.swift
//  Screen
//
//  Created by Natsu on 12/29/16.
//  Copyright © 2016 alex.chih. All rights reserved.
//

import UIKit

class NaAnima: NSObject
{
    func cloud(target:UIView, width:CGFloat, height:CGFloat, x0:CGFloat, y0:CGFloat, x1:CGFloat, y1:CGFloat)
    {
//        let cloud = UIImageView(image: UIImage(named: "cloud_white"))
//        cloud.frame = CGRect(x:-100, y:-100, width:width, height:height)
//        
//        target.addSubview(cloud)
//        
//        cloud.layer.shadowColor = UIColor.black.cgColor
//        cloud.layer.shadowOffset = CGSize(width: 2,height: 2);
//        cloud.layer.shadowOpacity = 0.4;//阴影透明度，默认0
//        cloud.layer.shadowRadius = 10;//阴影半径，默认3
//        
//        let ca = CABasicAnimation(keyPath: "position")
//        ca.duration = 16
//        ca.repeatCount = HUGE
//        
//        ca.fromValue = NSValue(cgPoint: CGPoint(x:x0, y:y0))
//        ca.toValue = NSValue(cgPoint: CGPoint(x:x1, y:y1))
//        
//        cloud.layer.add(ca, forKey: "move-layer")
//        
//        let getsure = UITapGestureRecognizer(target: nil, action: Selector(("Actiondo:")))
//        cloud.addGestureRecognizer(getsure)
        
        flyout(ufo: UIImage(named: "cloud_white")!, target: target, controller: nil, selector: nil, sequence: nil, width: width, height: height, x0: x0, y0: y0, x1: x1, y1: y1)
    }
    
    func sheep(target:UIView, controller:UITableViewController, selector:Selector?, width:CGFloat, height:CGFloat, x0:CGFloat, y0:CGFloat, x1:CGFloat, y1:CGFloat)
    {
        flyout(ufo: UIImage(named: "sheep")!, target: target, controller: controller, selector: selector, sequence: nil, width: width, height: height, x0: x0, y0: y0, x1: x1, y1: y1)
        
        NaAudio.ready()
    }
    
    func flyout(ufo:UIImage, target:UIView, controller:UITableViewController?, selector:Selector?, sequence:[Int32]?, width:CGFloat, height:CGFloat, x0:CGFloat, y0:CGFloat, x1:CGFloat, y1:CGFloat)
    {
        let button = UIButton()
        button.frame = CGRect(x:-100, y:-100, width:width, height:height)
        
        button.setImage(ufo, for: .normal)
        
        target.addSubview(button)
        
        button.addTarget(self, action:#selector(Go) , for: .touchUpInside)
        
//        let cloud = UIImageView(image: ufo)
//        cloud.frame = CGRect(x:-100, y:-100, width:width, height:height)
//        
//        target.addSubview(cloud)
        
        NaVisual.drawShadow(target: button)
        
        shake(target: button)
        scale(target: button)
        
        let ca = CABasicAnimation(keyPath: "position")
        ca.duration = 16
        ca.repeatCount = HUGE
        
        ca.fromValue = NSValue(cgPoint: CGPoint(x:x0, y:y0))
        ca.toValue = NSValue(cgPoint: CGPoint(x:x1, y:y1))
        
        button.layer.add(ca, forKey: "move-layer")
        
        let getsure = UITapGestureRecognizer(target: self, action:#selector(Go) )
        button.addGestureRecognizer(getsure)
    }
    
    func Go(sender:Any)
    {
        print("go go")
    }
    
    func shake(target:UIView)
    {
        let ca = CABasicAnimation(keyPath: "transform.rotation.z")
        
        ca.duration = 2.5 // 持续时间
        ca.repeatCount = HUGE // 重复次数
        
        ca.fromValue = NSNumber(floatLiteral:  M_PI)
        ca.byValue = NSNumber(floatLiteral: 0.0)
        ca.toValue = NSNumber(floatLiteral: -1 * M_PI)
        
        target.layer.add(ca, forKey: "rotate-layer")
    }
    
    func scale(target:UIView)
    {
        let ca = CABasicAnimation(keyPath: "transform.scale")
        
        // 动画选项设定
        ca.duration = 2.5 // 动画持续时间
        ca.repeatCount = HUGE // 重复次数
        ca.autoreverses = true // 动画结束时执行逆动画
        
        // 缩放倍数
        ca.fromValue = NSNumber(floatLiteral: 1.0)
        ca.toValue = NSNumber(floatLiteral: 2.0)
        
        target.layer.add(ca, forKey: "scale-layer")
    }
    
    

}
